﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MODEL;

namespace StudentManage
{
    public partial class Frm_Select : Form
    {
        public Frm_Select()
        {
            InitializeComponent();
        }

        private void Frm_Select_Load(object sender, EventArgs e)
        {
            
            this.dgv_ChaStu.DataSource = BLL.StuCardManage.StuCardService.ChaXunStudent();
 }

        private void dgv_ChaStu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            StuCard stuCard = new StuCard();
            stuCard.StuName = this.txt_Select.Text.Trim();
            if (this.txt_Select.Text != "")
            {
                dgv_ChaStu.DataSource = BLL.StuCardManage.StuCardService.ChaXunStudent2(stuCard);
            }
            else 
            {
                dgv_ChaStu.DataSource=BLL.StuCardManage.StuCardService.ChaXunStudent();
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Student frmstu = new Frm_Student();
            frmstu.ShowDialog();//将窗体显示为模态化窗体
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Update frm_Update = new Frm_Update(dgv_ChaStu);
            frm_Update.ShowDialog();
            
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StuCard stuCard = new StuCard();
            stuCard.StuId= dgv_ChaStu.SelectedRows[0].Cells["StuId"].Value.ToString();

            DialogResult dr = MessageBox.Show("确定要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (dr == DialogResult.Yes)
            {
                try
                { 
                //执行删除
                int num = BLL.StuCardManage.StuCardService.StuCardDelete(stuCard);

                if (num > 0)
                {
                    MessageBox.Show("操作成功");
                    dgv_ChaStu.DataSource = BLL.StuCardManage.StuCardService.ChaXunStudent();
                    //Frm_List.listBox1.Items.Add($"删除学号为：{dgv_ChaStu.SelectedRows[0].Cells["StuId"].Value.ToString()} 的学生信息，操作成功");                  
                }
                else
                {
                    MessageBox.Show("操作失败");
                    //Frm_List.listBox1.Items.Add($"删除学号为：{dgv_ChaStu.SelectedRows[0].Cells["StuId"].Value.ToString()} 的学生信息，操作失败");
                }
                }
                catch(Exception ex)
                {
                    //显示错误信息
                    MessageBox.Show(ex.Message);
                    //Frm_List.listBox1.Items.Add("执行操作时出现问题:\t\n" +ex.Message);
                }
            }
        }
    }
}
